const express = require('express');
const loginMiddleware = require("../middleware/LoginMiddleware");

const ExamineGroupsDao = require('../database/ExamineGroupsDao')

const router = express.Router();

router.post("/", loginMiddleware, async (req, res) => {
    let user = req.user;
    let group_id = req.body.group_id;
    try {
        let result = await ExamineGroupsDao.addExamineGroups(group_id, user.user_id);
        console.log("ExamineGroups添加的结果/是:\n" +JSON.stringify(result) + "\n————\n");
        res.send({ code: 2000, msg: "添加成功", data: null });
    } catch (err) {
        console.log(err);
        res.send({ code: 5000, msg: "添加失败", data: null });
    }
});

router.delete("/groupId/:GroupID", loginMiddleware, async (req, res) => {
    let GroupID = req.params.GroupID;
    try {
        let result = await ExamineGroupsDao.deleteExamineGroupsByGroupID(GroupID);
        console.log("ExamineGroups删除的结果/groupId/:GroupID是:\n" +JSON.stringify(result) + "\n————\n");
        res.send({ code: 2000, msg: "删除成功", data: null });
    }
    catch (err) {
        console.log(err);
        res.send({ code: 5000,msg: "删除失败", data: null });
    }
})

router.delete("/:ExamineGroupID", loginMiddleware, async (req, res) => {
    let ExamineGroupID = req.params.ExamineGroupID;
    try {
        let result = await ExamineGroupsDao.deleteExamineGroups(ExamineGroupID);
        console.log("ExamineGroups删除的结果/:ExamineGroupID是:\n" +JSON.stringify(result) + "\n————\n");
        res.send({ code: 2000, msg: "删除成功", data: null});
    }
    catch (err) {
        console.log(err);
        res.send({ code: 5000, msg: "删除失败", data: null });
    }
})

router.get("/:GroupID", loginMiddleware, async (req, res) => {
    let GroupID = req.params.GroupID;
    const pageIndex = req.query.pageIndex; // 从查询字符串中获取 pageIndex
    console.log("pageIndex:" + pageIndex);
    try {
        const start = 5 * (Number(pageIndex) - 1);
        let result = await ExamineGroupsDao.selectExamineGroupsByGroupID(start, 5, GroupID);
        console.log("ExamineGroups查询的结果/:GroupID是:\n" +JSON.stringify(result) + "\n————\n");
        res.send({ code: 2000, msg: "查询成功", data: result});
    }
    catch (err) {
        console.log(err);
        res.send({ code: 5000, msg: "查询失败", data: null });
    }
})

router.get("/:GroupID/:UserID", loginMiddleware, async (req, res) => {
    let GroupID = req.params.GroupID;
    let UserID = req.params.UserID;
    try {
        let result = await ExamineGroupsDao.selectExamineGroupsByGroupIDAndUserID(GroupID, UserID);
        console.log("ExamineGroups查询的结果/:GroupID/:UserID是:\n" +JSON.stringify(result) + "\n————\n");
        res.send({ code: 2000, msg: "查询成功", data: result});
    }
    catch (err) {
        console.log(err);
        res.send({ code: 5000, msg: "查询失败", data: null });
    }
})

router.get("/", loginMiddleware, async (req, res) => {
    let user = req.user;
    try {
        let result = await ExamineGroupsDao.selectExamineGroupsByUserID(user.user_id);
        console.log("ExamineGroups查询的结果/是:\n" +JSON.stringify(result) + "\n————\n");
        res.send({ code: 2000, msg: "查询成功", data: result });
    } catch(err) {
        console.log(err);
        res.send({ code: 5000, msg: "查询失败", data: null });
    }
})

module.exports = router;